<?php
namespace V1;
class E_Customer extends \ResourceClass {
    public $isActive = true;
    public $remark = '获取客户详情';
    public function get(array $req, array $post, &$result)
    {   
        if (!isset($req['uid'])) {
            return 'require user id ';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }
        if (!isset($req['enterpriseId'])) {
            return 'require enterpriseId';
        }
        if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $req['enterpriseId'], $req['t'], $req['k']) !== true) {
            return 'data illegal';
        }
        if (!isset($req['account'])) {
            return 'require customer account';
        }
        $result = array();
        if(array_key_exists('account',$req)&&!empty($req['account'])){  //获取客户详情
            $account = $req['account'];
            $enterprise_id = $req['enterpriseId'];

            $customer_cls = \Biz\EnterpriseCustomerHandler::getEnterpriseCustomerByAccount($account, $enterprise_id);
            if (!is_object($customer_cls)) {
                return '未找到客户信息';
            }
            $channel_cls = \Biz\ChannelHandler::getChannelById($customer_cls->getChannelId());
            if (!is_object($channel_cls)) {
                return '未找到渠道信息';
            }
            $customer['id'] = $customer_cls->getId();
            $customer['account'] = $customer_cls->getAccount();
            $customer['company'] = $customer_cls->getCompany();
            $customer['contact'] = $customer_cls->getContact();
            $customer['enterprise_id'] = $customer_cls->getEnterpriseId();
            $customer['customer_code'] = $customer_cls->getCustomerCode();
            $customer['pwd'] = $customer_cls->getPassword();
            $customer['tel'] = $customer_cls->getTelephone();
            // is_use 和 is_del 两者表示的是相反的,不要纠结,就是反的
            $customer['is_del'] = $customer_cls->getUseStatus() === \Cls\User\EnterpriseCustomer::USE_STATUS_ON ? \Cls\User\EnterpriseCustomer::USE_STATUS_OFF : \Cls\User\EnterpriseCustomer::USE_STATUS_ON;
            $customer['address'] = $customer_cls->getRegion()->getAddress();
            $customer['country'] = $customer_cls->getRegion()->getCountryId();
            $customer['countryname'] = $customer_cls->getRegion()->getCountryName();
            $customer['province'] = $customer_cls->getRegion()->getProvinceId();
            $customer['provincename'] = $customer_cls->getRegion()->getProvinceName();
            $customer['city'] = $customer_cls->getRegion()->getCityId();
            $customer['cityname'] = $customer_cls->getRegion()->getCityName();
            $customer['area'] = $customer_cls->getRegion()->getAreaId();
            $customer['areaname'] = $customer_cls->getRegion()->getAreaName();
            $customer['remark'] = $customer_cls->getRemark();
            $customer['channelname'] = $channel_cls->getName();
            $result = array($customer);
        }
        return '';
    }
    public function put(array $req, array $post, &$result){
        if (!isset($req['enterpriseId'])) {
            return 'require user enterpriseId ';
        }
        if (!isset($req['customerid'])) {
            return 'require user customerid ';
        }
        if (!isset($req['uid'])) {
            return 'require user id ';
        }
        if (!isset($req['userType'])) {
            return 'require  userType';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }
        if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $req['enterpriseId'], $req['t'] . $post['payload'], $req['k']) !== true) {
            return 'data illegal';
        }
        if(empty($post)){
            return 'require post data error';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data,$data);

        $enterprise_id = $req['enterpriseId'];
        $customer_id = $req['customerid'];
        $customer_cls = \Biz\EnterpriseCustomerHandler::getEnterpriseCustomerById($enterprise_id, $customer_id);
        if (isset($data['is_del'])) {
            // is_use 和 is_del 两者表示的是相反的,不要纠结,就是反的
            $use_status = (int)$data['is_del'] === \Cls\User\EnterpriseCustomer::USE_STATUS_ON ? \Cls\User\EnterpriseCustomer::USE_STATUS_OFF : \Cls\User\EnterpriseCustomer::USE_STATUS_ON;
            $customer_cls->setUseStatus($use_status);
        }
        if (\Biz\EnterpriseCustomerHandler::update($customer_cls) === true) {
            $result = true;
        } else {
            return '更新失败';
        }
        return '';        
    }
}